<?php

namespace App\Http\Controllers;

use App\Models\Adv;
use App\Models\Category;
use App\Models\Information;
use App\Models\Nav;

class IndexController extends Controller
{
    public $nav = [];

    public function __construct()
    {
        $this->nav = Nav::orderBy('order', 'asc')->get()->toArray();
    }

    public function index()
    {
        $flash = Information::where('image', '<>' , '')->orderBy('created_at', 'desc')->select(['id', 'title', 'image'])->limit(5)->get()->toArray();

        $data1 = Information::getInformations(Information::$catMap['GZDT'], 8);
        $data2 = Information::getInformations(Information::$catMap['TZGG'], 9);
        $data3 = Information::getInformations(Information::$catMap['JXYW'], 9);

        $data1_1 = [];
        if ($data1) {
            $data1_1 = current($data1);
            array_shift($data1);
        }

        $snjxyw_data = Information::getInformations(Information::$catMap['SNJXYW'], 6);
        $xqjxxx_data = Information::getInformations(Information::$catMap['XQJXXX'], 6);
        $dqgz_data = Information::getInformations(Information::$catMap['DQGZ'], 6);
        $gyjjfz_data = Information::getInformations(Information::$catMap['GYJJFZ'], 6);
        $xxcyfz_data = Information::getInformations(Information::$catMap['XXCYFZ'], 6);


        $adv = Adv::orderBy('order', 'asc')->get()->toArray();

        $data = compact('flash', 'data1', 'data1_1', 'data2', 'data3', 'snjxyw_data', 'xqjxxx_data', 'dqgz_data', 'gyjjfz_data', 'xxcyfz_data', 'adv');

        $data['nav'] = $this->nav;

        return view('http.index', $data);
    }

    //网站地图
    public function site()
    {
        $data = [];
        $row = Category::where('parent_id', 0)->get()->toArray();
        if ($row) {
            $ids = array_column($row, 'id');
            $sub_data = Category::whereIn('parent_id', $ids)->get()->toArray();

            foreach ($row as $key=>$val) {
                $data[$val['id']] = $val;
            }
            foreach ($sub_data as $key=>$val) {
                if (isset($data[$val['parent_id']]))
                    $data[$val['parent_id']]['sub'][] = $val;
            }
        }

        $data = [
            'data' => $data,
            'nav'  => $this->nav
        ];

        return view('http.site', $data);
    }
}